package com.outsourcing.bottle.db;

import android.content.ContentValues;
import android.database.Cursor;

import com.outsourcing.bottle.domain.PlatformAccount;

/**
 * 开放平台表
 * @author 06peng
 *
 */
public class PlatformAccountTable {

	public static final String TABLE_NAME = "platform_account";
	public static final String ID = "_id";
	public static final String OPEN_TYPE = "open_type";
	public static final String ACCESS_TOKEN = "access_Token";
	public static final String TOKEN_SECRET = "token_secret";
	public static final String NICK_NAME = "nickname";
	public static final String OPEN_UID = "open_uid";
	public static final String OPEN_SEX = "open_sex";
	public static final String OPEN_EXPIRE = "open_expire";
	public static final String OPEN_AVATAR = "open_avatar";
	
	private DBManagerImpl db = null;
	
	public PlatformAccountTable() {
		if (db == null) {
			db = DBManager.get();
		}
		if (!db.isTableExits(db.getConnection(), TABLE_NAME)) {
			createTable();
		}
	}
	
	private void createTable() {
		String createSql = "create table if not exists " + TABLE_NAME + " ("
				+ ID + " int primary key," + OPEN_TYPE + " int, "+NICK_NAME+" varchar, "
				+ ACCESS_TOKEN + " varchar," + TOKEN_SECRET + " varchar,"
				+ OPEN_UID + " varchar," + OPEN_SEX + " int," + OPEN_EXPIRE
				+ " varchar," + OPEN_AVATAR + " varchar)";
		db.creatTable(db.getConnection(), createSql, TABLE_NAME);
	}
	
	public void save(PlatformAccount account) {
		db.delete(db.getConnection(), TABLE_NAME, OPEN_TYPE + "=? and " + OPEN_UID + " = ?",
				new String[] { String.valueOf(account.getOpenType()), account.getOpenUid() });
		ContentValues values = new ContentValues();
		values.put(OPEN_TYPE, account.getOpenType());
		values.put(OPEN_UID, account.getOpenUid());
		values.put(OPEN_EXPIRE, account.getOpenExpire());
		values.put(OPEN_AVATAR, account.getOpenAvatar());
		values.put(OPEN_SEX, account.getOpenSex());
		values.put(NICK_NAME, account.getNickName());
		values.put(ACCESS_TOKEN, account.getAccessToken());
		values.put(TOKEN_SECRET, account.getTokenSecret());
		db.save(db.getConnection(), TABLE_NAME, values);
	}
	
	public PlatformAccount getPlatformAccount(int openType, String openUid) {
		Cursor cursor = db.find(db.getConnection(), "select * from " + TABLE_NAME
				+ " where " + OPEN_TYPE + "=? and " + OPEN_UID + "=?", new String[] { String.valueOf(openType), openUid });
		if (cursor == null)
			return null;
		PlatformAccount account = null;
		try {
			while (cursor.moveToNext()) {
				account = new PlatformAccount();
				account.setAccessToken(cursor.getString(cursor.getColumnIndex(ACCESS_TOKEN)));
				account.setNickName(cursor.getString(cursor.getColumnIndex(NICK_NAME)));
				account.setOpenAvatar(cursor.getString(cursor.getColumnIndex(OPEN_AVATAR)));
				account.setOpenExpire(cursor.getString(cursor.getColumnIndex(OPEN_EXPIRE)));
				account.setOpenSex(cursor.getInt(cursor.getColumnIndex(OPEN_SEX)));
				account.setOpenType(cursor.getInt(cursor.getColumnIndex(OPEN_TYPE)));
				account.setOpenUid(cursor.getString(cursor.getColumnIndex(OPEN_UID)));
				account.setTokenSecret(cursor.getString(cursor.getColumnIndex(TOKEN_SECRET)));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null && !cursor.isClosed()) {
				cursor.close();
			}
		}
		return account;
	}
}
